package duxiaoman;

import java.util.Scanner;

/**
 * @author Hang
 * @date 2022/8/31
 * @project AutumnOffer
 * @description
 **/
public class Solution2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        for(int i=0;i<num;i++){
            int n = sc.nextInt();
            int m = sc.nextInt();
            int k = sc.nextInt();
            int x = sc.nextInt();
            int[][] arr = new int[n][m];
            boolean[][] vis = new boolean[n][m];
            for(int j1=0;j1<n;j1++){
                for(int j2=0;j2<m;j2++){
                    arr[j1][j2] = sc.nextInt();
                }
            }
            System.out.println(dfs(arr,0,0,0,x,vis));
        }
    }
    public static boolean dfs(int[][] arr,int x,int y,int sum,int k,boolean[][] vis){
        if(x==arr.length&&y< arr[0].length){
            return dfs(arr,x,y+1,sum,k,vis);
        }
        if(y==arr[0].length&&x < arr.length){
            return dfs(arr,x+1,y,sum,k,vis);
        }
        if(x> arr.length&&y>arr[0].length){
            return false;
        }
        if(x==arr.length&&y==arr[0].length&&sum==k){
            return true;
        }
        vis[x][y] = true;
        sum += arr[x][y];
        boolean dfs = dfs(arr, x, y + 1, sum, k,vis)||dfs(arr, x + 1, y, sum, k,vis);
        sum -= arr[x][y];
        vis[x][y] = false;
        return dfs;
    }
}
